package br.com.felipeab.dexter.faces;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 * Classe BasicController básica para funcionamento de uma tela
 * 
 * @author felipea
 * 
 * @param <T>
 */
public abstract class BasicController<T extends Serializable> implements Serializable {

	private static final long serialVersionUID = -4198803381139997834L;

	private List<T> list;
	private DataModel<T> model;
	private T rowData;

	/**
	 * Obtém a lista de dominio
	 * 
	 * @return
	 */
	public List<T> getList() {
		if (list == null) {
			list = new ArrayList<T>();
		}
		return list;
	}

	/**
	 * Atribui a lista de dominio
	 * 
	 * @param list
	 */
	public void setList(List<T> list) {
		this.model = new ListDataModel<T>(list);
		this.list = list;
	}

	public DataModel<T> getModel() {
		if (model == null) {
			model = new ListDataModel<T>(getList());
		}
		return model;
	}

	public void setModel(DataModel<T> model) {
		this.model = model;
	}

	public T getRowData() {
		return rowData;
	}

	public void setRowData(T rowData) {
		this.rowData = rowData;
	}

	public void msgInfo(String summary) {
		MessageUtil.addInfoMessage(summary);
	}

	public void msgWarn(String summary) {
		MessageUtil.addWarnMessage(summary);
	}

	public void msgError(String summary) {
		MessageUtil.addErrorMessage(summary);
	}

	public void msgFatal(String summary) {
		MessageUtil.addFatalMessage(summary);
	}
}
